from django.shortcuts import render
from django.http import HttpResponse
from django.core.mail import send_mail
from django.conf import settings

from . import models
from . import forms


def register(request):
    form = forms.RegisterForm()
    context = {'form': form}
    return render(request, 'app01/register.html', context)

def sendmail(request):
    """
    def send_mail(subject, message, from_email, recipient_list,
                fail_silently=False, auth_user=None, auth_password=None,
                connection=None, html_message=None):
    """
    link = "https://www.baidu.com" # 不要忽略 https:// 协议申明，否则邮件中无法跳转
    subject = 'django app01 send mail test.'
    message_fmtstr = """
    邮箱验证测试，<a href="{0}">点击验证</a>
    若无法跳转，请复制下面的链接到浏览器打开：
    {0}
    """
    html_message = message_fmtstr.format(link)
    to_list = ['vailete@163.com',]
    # send_mail 函数返回值为 0 或 1, 对应成功发送信息的数量
    ret = send_mail(subject=subject,
                    message='', # 使用 html_message 时，为空
                    from_email=settings.EMAIL_HOST_USER,
                    recipient_list=to_list,
                    html_message=html_message)

    return HttpResponse(ret)

